#include <iostream>

using namespace std;
int n;
const int N = 100010;
int acid[N];
int bitter[N];
bool st[N];
int min_sub = 1e9;
void dfs(int x, int mul, int sum)
{
    if ( x > n )
    {
        bool has_selected = false;
        for (int i = 1; i <= n; i++)
        { 
            if (st[i])
            { 
                has_selected = true;
                break;
            }
        }
        if (has_selected)
        {
            int diff = abs(mul - sum);
            min_sub = min(diff, min_sub);
        }
        return;
    }

    //选
    st[x] = true;
    dfs(x + 1, mul * acid[x], sum + bitter[x]);
    // 不选
    st[x] = false;
    dfs(x + 1, mul, sum);
}
int main()
{
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> acid[i] >> bitter[i];
    }
    dfs(1, 1, 0);
    cout << min_sub << endl;
    return 0;
}